using System;
using Microsoft.Xna.Framework.Input;
using SolarWinds.Helpers;

namespace SolarWinds.Input
{
    struct ButtonStateOldNew
    {
        public ButtonState OldState;
        public ButtonState NewState;

        public ButtonStateOldNew(ButtonState oldState, ButtonState newState)
        {
            OldState = oldState;
            NewState = newState;
        }
    }

    public class InputManager : ComponentManager, IInputManagerService
    {
        private readonly MouseManager mMouseManager;
        private readonly KeyboardManager mKeyboardManager;

        private TimeSpan mClickSpan = new TimeSpan(0, 0, 1);
        private TimeSpan mDoubleClickSpan = new TimeSpan(0, 0, 2);
  
        #region Properties

        public KeyboardManager Keyboard
        {
            get { return mKeyboardManager; }
        }

        public MouseManager Mouse
        {
            get { return mMouseManager; }
        }

        public TimeSpan ClickSpan
        {
            get { return mClickSpan; }
            set { mClickSpan = value; }
        }

        public TimeSpan DoubleClickSpan
        {
            get { return mDoubleClickSpan; }
            set { mDoubleClickSpan = value; }
        }

        #endregion

        public InputManager(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IInputManagerService), this);

            mMouseManager = new MouseManager(game);
            Components.Add(mMouseManager);
            mKeyboardManager = new KeyboardManager(game);
            Components.Add(mKeyboardManager);
        }

        public static InputModifiers GetModifiers()
        {
            KeyboardState newKeyState = Microsoft.Xna.Framework.Input.Keyboard.GetState();

            // get modifiers
            InputModifiers modifiers = InputModifiers.None;

            if (newKeyState.IsKeyDown(Keys.RightShift) ||
                newKeyState.IsKeyDown(Keys.LeftShift))
            {
                modifiers |= InputModifiers.Shift;
            }
            if (newKeyState.IsKeyDown(Keys.RightControl) ||
                newKeyState.IsKeyDown(Keys.LeftControl))
            {
                modifiers |= InputModifiers.Control;
            }
            if (newKeyState.IsKeyDown(Keys.RightAlt) ||
                newKeyState.IsKeyDown(Keys.LeftAlt))
            {
                modifiers |= InputModifiers.Alt;
            }
            if (newKeyState.IsKeyDown(Keys.RightWindows) ||
                newKeyState.IsKeyDown(Keys.LeftWindows))
            {
                modifiers |= InputModifiers.Meta;
            }

            return modifiers;
        }
    }
}